#include "chat_module.h"
#include "GameServer.h"
#include "ManagerHelper.h"
#include "../share/MessageDef.h"
#include "../share/dirtyword.h"
#include "chat.h"

void CChatModule::Initialize()
{
	//registe_client_msg(player_union_changed_notify_chat_server, CChatModule::SPlayerChangedInGuildNotify);
	//registe_client_msg(player_login_chat_server, CChatModule::SLoginChatSrvReq);
	//registe_client_msg(union_changed_notify_chat_server, CChatModule::SGuildChangedNotify);
	registe_client_msg(player_friend_chat_message_req, CChatModule::SChatToFriendMessageReq);
	//registe_client_msg(common_server_chat_record_req, CChatModule::SChatRecordReq);
	registe_client_msg(player_chat_message, CChatModule::SChatMessageReq);
}
//
//void CChatModule::SPlayerChangedInGuildNotify(Player& player, const CJsonMessageBase& msg)
//{
//	const Json::Value& m_jv = msg.GetJsonValue();
//	if (!m_jv.isMember("msgid"))
//	{
//		return;
//	}
//
//	uint32_t _accountId = m_jv["account"].asUInt();
//
//	if (_accountId > 0)
//	{
//		uint32_t _playerId = m_jv["playerId"].asUInt();
//		uint32_t _type = m_jv["arg1"].asUInt();
//		uint32_t _guildId = m_jv["arg2"].asUInt();
//		uint32_t _memberId = m_jv["arg3"].asUInt();
//
//		ChatPlayerMgrPtr->OprationPlayerInGuild(_memberId, _type, _guildId);
//	}
//}
//
//void CChatModule::SLoginChatSrvReq(Player& player, const CJsonMessageBase& msg)
//{
//	const Json::Value& m_jv = msg.GetJsonValue();
//	if (!m_jv.isMember("msgid"))
//	{
//		return;
//	}
//
//	uint32_t _accountId = m_jv["account"].asUInt();
//
//	if (_accountId > 0)
//	{
//		uint32_t _playerId = m_jv["playerId"].asUInt();
//		uint32_t _postionId = m_jv["arg1"].asUInt();
//		uint32_t _areaId = m_jv["arg2"].asUInt();
//		uint32_t _vipId = m_jv["arg3"].asUInt();
//		uint32_t _unionId = m_jv["arg4"].asUInt();
//		uint32_t _heroId = 0;//m_jv["arg5"].asUInt();
//		std::string _nickName = m_jv["arg5"].asString();
//
//		//这个现在为了建荣贵，把socketid放着，以后做新功能不能带这个
//		uint32_t _sock_id = msg.GetFromSocketId();
//		ChatPlayerMgrPtr->AddNewChatPlayer(_accountId, _playerId, _sock_id, _postionId
//			, _areaId, _vipId, _unionId, _heroId, _nickName.c_str());
//
//		//检查公会及成员内存数据
//		if (_unionId > 0)
//		{
//			bool _is_exist = ChatPlayerMgrPtr->CheckGuildIsExist(_unionId);
//			if (!_is_exist)
//			{
//				ChatPlayerMgrPtr->OprationForGuild(1/*type_creat_guild*/, _unionId);
//			}
//
//			ChatPlayerMgrPtr->OprationPlayerInGuild(_playerId, 1/*type_player_join_guild*/, _unionId);
//		}
//
//	}
//}
//
//void CChatModule::SGuildChangedNotify(Player& player, const CJsonMessageBase& msg)
//{
//	const Json::Value& m_jv = msg.GetJsonValue();
//	if (!m_jv.isMember("msgid"))
//	{
//		return;
//	}
//
//	uint32_t _accountId = m_jv["account"].asUInt();
//
//	if (_accountId > 0)
//	{
//		uint32_t _playerId = m_jv["playerId"].asUInt();
//		uint32_t _type = m_jv["arg1"].asUInt();
//		uint32_t _guildId = m_jv["arg2"].asUInt();
//
//		uint32_t _sock_id = msg.GetFromSocketId();
//		ChatPlayerMgrPtr->OprationForGuild(_type, _guildId);
//
//		bool _is_exist = ChatPlayerMgrPtr->CheckGuildIsExist(_guildId);
//		if (_is_exist)
//		{
//			ChatPlayerMgrPtr->OprationPlayerInGuild(_playerId, 1/*type_player_join_guild*/, _guildId);
//		}
//	}
//}
//
void CChatModule::SChatToFriendMessageReq(Player& player, const CJsonMessageBase& msg)
{
	LOG_DEBUG("default", "SChatToFriendMessageReq start.");
	const Json::Value& m_jv = msg.GetJsonValue();
	if (!m_jv.isMember("msgid"))
	{
		return;
	}

	uint32_t _accountId = m_jv["account"].asUInt();
	uint32_t _playerId = m_jv["playerId"].asUInt();
	uint32_t _type = m_jv["arg1"].asUInt();
	std::string _content = m_jv["arg2"].asString();
	uint32_t _destId = m_jv["arg3"].asUInt();
	uint32_t _contentType = m_jv["arg4"].asUInt();

	uint32_t _heroId = m_jv["arg5"].asUInt();
	std::string _name = m_jv["arg6"].asString();

	//LOG_DEBUG("default","SChatToFriendMessageReq step.playerID:[%d]", _playerId);

	/*G——voice*/
	std::string _gvid = ""; // 语音ID
	double _gvti = 0.00;	// 语音时长
	std::string _gvte = "";	// 语音翻译

	if (m_jv.isMember("arg7"))
	{
		_gvid = m_jv["arg7"].asString();
		LOG_DEBUG("default", "SChatToFriendMessageReq step._gvid:[%s]", _gvid.c_str());
	}

	if (m_jv.isMember("arg8"))
	{
		_gvti = m_jv["arg8"].asDouble();
		LOG_DEBUG("default", "SChatToFriendMessageReq step._gvti:[%lf]", _gvti);
	}

	if (m_jv.isMember("arg9"))
	{
		_gvte = m_jv["arg9"].asString();
		LOG_DEBUG("default", "SChatToFriendMessageReq step._gvte:[%s]", _gvte.c_str());
	}

	CChatManager::GetInstance().SendChatToFriendMessageToClient(_playerId, _type, _destId, _content.c_str(), _contentType, player
		, _gvid.c_str(), _gvti, _gvte.c_str());

	LOG_DEBUG("default", "SChatToFriendMessageReq end.");
}


void CChatModule::SChatMessageReq(Player& player, const CJsonMessageBase& msg)
{
	const Json::Value& m_jv = msg.GetJsonValue();
	if (!m_jv.isMember("msgid"))
	{
		return;
	}

	

	uint32_t _accountId = m_jv["account"].asUInt();
	uint32_t _playerId = m_jv["playerId"].asUInt();
	uint32_t _type = m_jv["arg1"].asUInt(); // 聊天类型 1世界聊天(type_chat_to_world)；2工会聊天(type_chat_to_guild)；3私聊(type_chat_to_player)；4附近聊天；5队伍聊天；系统聊天
	//std::string _content = m_jv["arg2"].asString();
	std::string _content = m_jv["arg2"].asString();
	uint32_t _destId = m_jv["arg3"].asUInt();
	uint32_t _heroId = m_jv["arg4"].asUInt();
	std::string _name = m_jv["arg5"].asString();

	

	/*G——voice*/
	std::string _gvid = ""; // 语音ID
	double _gvti = 0.00;	// 语音时长
	std::string _gvte = "";	// 语音翻译

	

	if (m_jv.isMember("arg6"))
	{
		_gvid = m_jv["arg6"].asString();
	}

	if (m_jv.isMember("arg7"))
	{
		_gvti = m_jv["arg7"].asDouble();
	}

	if (m_jv.isMember("arg8"))
	{
		_gvte = m_jv["arg8"].asString();
	}

	//使用喇叭类型
	uint16_t hornType = m_jv["arg9"].asUInt();//使用大喇叭^v^ (0:无喇叭, 1:小喇叭, 2:大喇叭)

											  //分享类型
	uint16_t shareType = 0;
	if (m_jv.isMember("arg10"))
	{
		shareType = m_jv["arg10"].asUInt();//^v^ (0:普通聊天, 1:角斗场, 2:装备)
	}

	//ChatPlayer* pMyself = ChatPlayerMgrPtr->GetChatPlayer(_playerId);
	//if (pMyself == NULL)
	//{
	//	return;
	//}

	

	if (CDirtyWord::GetInstance().OI_Dirty_Check(3, (char*)_content.c_str(), 1) != 0)
	{
		LOG_WARN("default", "name have dirtyword[%s]!", _content.c_str());
	}

	////同步玩家实时信息
	//pMyself->SetHeroId(_heroId);
	//pMyself->SetNickname(_name.c_str());

	if (_gvid.length() > 0)
	{
		CChatManager::GetInstance().SendChatMessageToClient(_playerId, _type, _destId, _content.c_str(), player, _gvid.c_str(), _gvti, _gvte.c_str());
	}
	else
	{
		CChatManager::GetInstance().SendChatMessageToClient(_playerId, _type, _destId, _content.c_str(), player, hornType, shareType);
	}

	// GameServer关注的事件
	if (hornType == 1)
	{
		CChatManager::GetInstance().DealChatEvent(_playerId, 1, nullptr);
	}
	if (hornType == 2)
	{
		CChatManager::GetInstance().DealChatEvent(_playerId, 2, nullptr);
	}

	if (_type == 1)
	{
		if (_gvid == "")
		{
			CChatManager::GetInstance().DealChatEvent(_playerId, 3, nullptr);

			//if (_content.find("我来了") != std::string::npos)
			//{
			//	CChatManager::GetInstance().DealChatEvent(_playerId, 5, nullptr);
			//}
		}
		else
		{
			CChatManager::GetInstance().DealChatEvent(_playerId, 4, nullptr);
		}
	}

	if (_type == 2)
	{
		if (_gvid == "")
		{
			CChatManager::GetInstance().DealChatEvent(_playerId, 6, nullptr);
		}
		else
		{
			CChatManager::GetInstance().DealChatEvent(_playerId, 7, nullptr);
		}
	}
}
